import { apiTsgl, apiInvmb, apiTsglAdd } from '@/api/url/libraryModules';
import router from '@/router';
import { isNullPm, isParam } from '@/utils/baseMethods';
import Message from '@arco-design/web-vue/es/message';
import { reactive } from 'vue';
import { Notification } from '@arco-design/web-vue';
export default function importConfig() {
  const base = reactive<any>({
    basics: null,
    visible: false,
    title: null,
    fileList: null,
    typeValue: 'cptz',
    ossList: {
      cptz: null,
      zyzds: null,
      jyzds: null,
      jytz: null,
    },
  });

  const init = () => {
    getTable();
  };

  /**
   * @exports getTable
   */
  const getTable = () => {
    const steam = {
      MB001: router.currentRoute.value.query.UID,
    };
    apiInvmb(steam).then((res: any) => {
      if (res.code === 1) {
        getBasics(res.data);
      }
    });
    apiTsgl(steam).then((res: any) => {
      base.ossList.cptz = JSON.parse(res.data[0].cptzlist);
      base.ossList.zyzds = JSON.parse(res.data[0].zyzdslist);
      base.ossList.jyzds = JSON.parse(res.data[0].jyzdslist);
      base.ossList.jytz = JSON.parse(res.data[0].jytzlist);
    });
  };

  const getBasics = (param) => {
    base.basics = [
      {
        label: '品名',
        value: param.mb001,
        align: '',
      },
      {
        label: '品号',
        value: param.mb002,
      },
      {
        label: '规格',
        value: param.mb003,
      },
      {
        label: '库存单位',
        value: param.mb004,
      },
      {
        label: '品号分类一',
        value: param.mb005,
      },
      {
        label: '品号分类二',
        value: param.mb006,
      },
      {
        label: '品号分类三',
        value: param.mb007,
      },
      {
        label: '品号分类四',
        value: param.mb008,
      },
      {
        label: '商品描述',
        value: param.mb009,
      },
      {
        label: '标准工艺路线品号',
        value: param.mb010,
      },
      {
        label: '标准工艺路线编号',
        value: param.mb011,
      },
      {
        label: '文档编号',
        value: param.mb012,
      },
      {
        label: '条码编号',
        value: param.mb013,
      },
      {
        label: '单位净重',
        value: param.mb014,
      },
      {
        label: '重量单位',
        value: param.mb015,
      },
      {
        label: '外包装单位',
        value: param.mb016,
      },
      {
        label: '主要仓库',
        value: param.mb017,
      },
    ];
  };
  const onUpDate = (param, key) => {
    base.typeValue = key;
    base.title = param;
    base.visible = true;
  };

  const IMAGEKIND = (param) => {
    const steam = [
      {
        value: 'cptz',
        label: '产品图纸',
      },
      {
        value: 'zyzds',
        label: '作业指导书',
      },
      {
        value: 'jyzds',
        label: '检验指导书',
      },
      {
        value: 'jytz',
        label: '检验图纸',
      },
    ];
    if (param) {
      return isParam(steam, 'value', param).value;
    } else {
      return steam;
    }
  };

  const selectDoom = (param) => {
    console.log(param, '==>typeValue');
    base.typeValue = param;
  };

  const uploadPM = (param: any) => {
    base.fileList = param;
  };

  const handleOk = (done): any => {
    if (isNullPm(base.fileList)) {
      Notification.error('请先确认上传文件');
      done(false);
      return false;
    }
    const steam: any = {
      mb001: router.currentRoute.value.query.UID,
    };
    let iilist: any = null;
    base.fileList.map((ren: any) => {
      if (isNullPm(iilist)) iilist = [];
      iilist.push({
        name: ren.name,
        type: base.typeValue,
        url: ren.url,
      });
    });

    if (base.title == '产品图纸') {
      steam.cptzlist = JSON.stringify(iilist);
    } else if (base.title == '作业指导书') steam.zyzdslist = JSON.stringify(iilist);
    else if (base.title == '检验指导书') steam.jyzdslist = JSON.stringify(iilist);
    else steam.jytzlist = JSON.stringify(iilist);

    apiTsglAdd(steam).then((res: any) => {
      Notification.success('保存成功');
      done();
      init();
    });
  };

  /**
   * @exports translation
   */
  const translation = (param) => {
    let steam = false;
    if (param.indexOf('pdf') == -1) {
      steam = true;
    }
    return steam;
  };

  const goToRouter = (param) => {
    window.open(param);
  };

  const imgListPla = () => {
    if (base.title == '产品图纸') {
      return base.ossList.cptz;
    } else if (base.title == '作业指导书') return base.ossList.zyzds;
    else if (base.title == '检验指导书') return base.ossList.jyzds;
    else return base.ossList.jytz;
  };

  return {
    base,
    init,
    onUpDate,
    IMAGEKIND,
    selectDoom,
    uploadPM,
    handleOk,
    translation,
    goToRouter,
    imgListPla,
  };
}
